<?php
class indexAction extends backendAction {

    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('menu');
    }

    public function index() {
    	$menu = D('menu')->where(array('module_name'=>'setting','action_name'=>'index','pid'=>0))->find();
        $pid = $menu['id'];
        $menu = D('menu')->where(array('module_name'=>'setting','action_name'=>'index','pid'=>$pid))->find();
        $id = $menu['id'];
		$this->redirect('setting/index', array('menuid' => $id,'menupid'=>$pid));
        //$this->display();
    }

    public function panel() {
        $message = array();
        $this->assign('message', $message);
        $system_info = array(
            'zyb_version' => ZYB_VERSION . ' RELEASE '. ZYB_RELEASE .' [<a href="http://www.nksoft.cn/" class="blue" target="_blank">查看最新版本</a>]',
            'server_domain' => $_SERVER['SERVER_NAME'] . ' [ ' . gethostbyname($_SERVER['SERVER_NAME']) . ' ]',
            'server_os' => PHP_OS,
            'web_server' => $_SERVER["SERVER_SOFTWARE"],
            'php_version' => PHP_VERSION,
            'mysql_version' => mysql_get_server_info(),
            'upload_max_filesize' => ini_get('upload_max_filesize'),
            'max_execution_time' => ini_get('max_execution_time') . '秒',
            'safe_mode' => (boolean) ini_get('safe_mode') ?  L('yes') : L('no'),
            'zlib' => function_exists('gzclose') ?  L('yes') : L('no'),
            'curl' => function_exists("curl_getinfo") ? L('yes') : L('no'),
            'timezone' => function_exists("date_default_timezone_get") ? date_default_timezone_get() : L('no')
        );
        $this->assign('system_info', $system_info);
        $this->display();
    }

     public function login() {
        if (IS_POST) {
            $username = $this->_post('username', 'trim');
            $password = $this->_post('password', 'trim');
            $verify_code = $this->_post('verify_code', 'trim');
            $vcode=$this->_post('vcode','trim');
        	$btime=$this->_post('btime','trim');
        	if($btime<(time()-1200))$this->error('验证码超时!');
        	if($vcode!=md5(md5(C('Admin_Enter_Md5')).$btime)){
        		$this->error('数据校验失败,请退出重进！！');
        	}
            if($this->verify($verify_code)){
                $this->error('很抱歉,验证码不正确哦！！!');
            }
            $admin = M('admin')->where(array('username'=>$username, 'status'=>1))->find();
            if (!$admin) {
                $this->error('用户名或密码不正确！！');
            }
			
            if ($admin['password']!= md5(base64_encode($password))) {
                $this->error('用户名或密码不正确!!');
            }
            session('admin', array(
                'id' => $admin['id'],
                'username' => $admin['username'],
                'role_id' => $admin['role_id'],
            ));
            session("verify",null);
            M('admin')->where(array('id'=>$admin['id']))->save(array('last_time'=>time(), 'last_ip'=>get_client_ip()));
            $this->success('登录成功,正在转入后台管理中心....', U('index/index'));
        } else {
                //var_dump(session('admin'));die;
                if(session('admin')){
                    $this->success('登录成功,正在转入后台管理中心....', U('index/index'));
                }else{
					$vcode=$this->_get('vcode','trim');
					$btime=$this->_get('btime','trim');
					if($vcode!=md5(md5(C('Admin_Enter_Md5')).$btime)){
						$this->error('身份验证错误！！');
					}
					$this->assign(array('vcode'=>$vcode,'btime'=>$btime));
					$this->display();
				}
        	
        }
    }

    public function logout() {
        session('admin', null);
        $this->success('退出成功!', U('home/index/index'));
        exit;
    }

    public function verify_code() {

        $checkcode = new Checkcode();
        $checkcode->code_len =6;
        $checkcode->font_size =18;
        $checkcode->width = 130;
        $checkcode->height = 30;
        $checkcode->doimage();
        //验证码类型
        $verify=$checkcode->get_code();
        session("verify",$verify);
        exit();
    }

   public function verify($verify) {
   	    if(!$verify)return true;
        $verifyArr = session("verify");
        if ($verifyArr == strtolower($verify)) {
            session("verify",null);
            return false;
        } else {
            return true;
        }
    }
  


    public function often() {
        if (isset($_POST['do'])) {
            $id_arr = isset($_POST['id']) && is_array($_POST['id']) ? $_POST['id'] : '';
            $this->_mod->where(array('often'=>1))->save(array('often'=>0));
            $id_str = implode(',', $id_arr);
            if($id_str)$this->_mod->where('id IN('.$id_str.')')->save(array('often'=>1));
            $this->success(L('operation_success'));
        } else {
            $r = $this->_mod->admin_menu(0);
            $list = array();
            foreach ($r as $v) {
                $v['sub'] = $this->_mod->admin_menu($v['id']);
                foreach ($v['sub'] as $key=>$sv) {
                    $v['sub'][$key]['sub'] = $this->_mod->admin_menu($sv['id']);
                }
                $list[] = $v;
            }
            $this->assign('list', $list);
            $this->display();
        }
    }

    public function map() {
        $r = $this->_mod->admin_menu(0);
        $list = array();
        foreach ($r as $v) {
            $v['sub'] = $this->_mod->admin_menu($v['id']);
            foreach ($v['sub'] as $key=>$sv) {
                $v['sub'][$key]['sub'] = $this->_mod->admin_menu($sv['id']);
            }
            $list[] = $v;
        }
        $this->assign('list', $list);
        $this->display();
    }
     //定时访问函数更改名片等信息
   public function timing(){
       set_time_limit(0);
        //查询名片信息
        $list = D('card')->where(array('etime'=>array('neq',0),'status'=>array('neq',0)))->field('uid,etime,wxusername')->select();
        foreach($list as $k=>$v){
            if($v['etime']<time()){
                //过期更改名片状态，发送站内消息
                D('card')->where(array('uid'=>$v['uid']))->save(array('status'=>0,'level'=>1));
                $suser['id'] = $v['uid'];
                $suser['username'] = $v['wxusername'];
                sys_send_msg($suser,'名片过期提醒','您的名片于'.date("Y年m月d日", $v['etime']).'已经过期，为保证您的正常使用，请及时续费。',6);
            }
        }

   }
}